package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.utils.EventListenerUtil;
    import components.utils.TimeoutIntervalUtil;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.clearInterval;
    import flash.utils.clearTimeout;
    import flash.utils.setInterval;
    import flash.utils.setTimeout;

	[Event(name="change",type="flash.events.Event")]
    public class BaseNumericStepper extends UIComponent
    {
        private var _value:Number = 0;

        private var _maxValue:Number = 100;

        private var _minValue:Number = 0;

        private var _step:Number = 1;

        private var _timeOutID:uint;

        private var _intervalID:uint;

        public function BaseNumericStepper()
        {
            super();

			EventListenerUtil.addEventListener(upArrowButton,MouseEvent.MOUSE_DOWN, upArrowButton_mouseDownHandler);
			EventListenerUtil.addEventListener(upArrowButton,MouseEvent.MOUSE_UP, cleanTween);
			EventListenerUtil.addEventListener(upArrowButton,MouseEvent.ROLL_OUT, cleanTween);

			EventListenerUtil.addEventListener(downArrowButton,MouseEvent.MOUSE_DOWN, downArrowButton_mouseDownkHandler);
			EventListenerUtil.addEventListener(downArrowButton,MouseEvent.MOUSE_UP, cleanTween);
			EventListenerUtil.addEventListener(downArrowButton,MouseEvent.ROLL_OUT, cleanTween);

			EventListenerUtil.addEventListener(textInput,Event.CHANGE, textInput_changeHandler);
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get textInput():BaseTextInput
        {
            return getChildByName("textInputSkin") as BaseTextInput;
        }

        public function get upArrowButton():BaseButton
        {
            return getChildByName("upArrowButtonSkin") as BaseButton;
        }

        public function get downArrowButton():BaseButton
        {
            return getChildByName("downArrowButtonSkin") as BaseButton;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
        }

        protected override function measureSizes():Boolean
        {
            if (super.measureSizes())
            {
                textInput.width = width - textInput.x - upArrowButton.width - 2;

                upArrowButton.x = width - upArrowButton.width;
                downArrowButton.x = width - downArrowButton.width;

                return true;
            }
            return false;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();

            textInput.text = value.toString();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/


        [Inspectable(defaultValue="0")]
        public function get value():Number
        {
            return _value;
        }

        public function set value(v:Number):void
        {
			if(value==v)
				return ;
			
            _value = v;

            checkValue();

			dispatchEvent(new Event(Event.CHANGE));
            invalidate();
        }

        [Inspectable(defaultValue="0")]
        public function get maxValue():Number
        {
            return _maxValue;
        }

        public function set maxValue(value:Number):void
        {
            _maxValue = value;
            checkValue();
            invalidate();
        }

        [Inspectable(defaultValue="0")]
        public function get minValue():Number
        {
            return _minValue;
        }

        public function set minValue(value:Number):void
        {
            _minValue = value;
            checkValue();
            invalidate();
        }

        [Inspectable(defaultValue="1")]
        public function get step():Number
        {
            return _step;
        }

        public function set step(value:Number):void
        {
            _step = value;
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        private function checkValue():void
        {
            _value = Math.max(minValue, _value);
            _value = Math.min(maxValue, _value);
        }

        protected function upArrowButton_mouseDownHandler(event:MouseEvent):void
        {
            value += step;
            _timeOutID = TimeoutIntervalUtil.addSetTimeout(this,btnTween, _delayTime, true);
        }

        protected function downArrowButton_mouseDownkHandler(event:MouseEvent):void
        {
            value -= step;
            _timeOutID = TimeoutIntervalUtil.addSetTimeout(this,btnTween, _delayTime, false);
        }

        /**
         *
         * @param flag true:向下滚动，false:向上滚动
         *
         */
        protected function btnTween(flag:Boolean):void
        {
            clearTimeout(_timeOutID);
            _intervalID =TimeoutIntervalUtil.addSetInterval(this,btnDown, 50, flag);
        }

        protected function btnDown(flag:Boolean):void
        {
            if (flag)
                value += step;
            else
                value -= step;

            if (value <= minValue || value >= maxValue)
                clearInterval(_intervalID);
        }

        protected function cleanTween(event:MouseEvent):void
        {
            clearInterval(_intervalID);
            clearTimeout(_timeOutID);
        }

        protected function textInput_changeHandler(event:Event):void
        {
            value = Number(textInput.text);
        }

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

            textInput.focusComp = focusComp;
            upArrowButton.focusComp = focusComp;
            downArrowButton.focusComp = focusComp;
        }
		
		public override function destroy():void
		{
			super.destroy();
		}
		
		public override function validateNow():void
		{
			super.validateNow();
			textInput.validateNow();
		}
    }
}